#pragma once
#include <iostream>
#include <ctime>
#include <unistd.h>
#include <cstdarg>

// 错误等级
#define DEBUG 0
#define NORMAL 1
#define WARNING 2
#define ERROR 3
#define FATAL 4

const char *to_levelstr(int level)
{
    switch (level)
    {
    case DEBUG:
        return "DEBUG";
    case NORMAL:
        return "NORMAL";
    case WARNING:
        return "WARNING";
    case ERROR:
        return "ERROR";
    case FATAL:
        return "FATAL";

    default:
        return "UNKNOW";
    }
}

void logMessage(int level, const char *format, ...)
{
    // 初始化日志前缀
    char logPre[1024];
    snprintf(logPre, sizeof(logPre), "[%s][%ld][pid:%d]", to_levelstr(level), (long int)time(nullptr), getpid());

    // 填写内容
    char logContent[1024];
    va_list arg;
    va_start(arg, format);
    vsnprintf(logContent, sizeof(logContent), format, arg);

    // 直接打印
    std::cout << logPre << logContent << std::endl;
}